package com.aebiz.usercenter.users.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.aebiz.usercenter.common.util.ConstatFinalUtil;
import com.aebiz.usercenter.common.util.PageInfoUtil;
import com.aebiz.usercenter.users.dao.IAAdminsDao;
import com.aebiz.usercenter.users.dao.IARoleDao;
import com.aebiz.usercenter.users.pojo.AAdmins;
import com.aebiz.usercenter.users.pojo.ARole;
import com.aebiz.usercenter.users.service.IUsersService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 用户Service的实现类
 * @author WangshSxt
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJSON=new JSONObject();
		/*按照用戶姓名查詢*/
		Map<String, Object> condMap=new HashMap<>();
		condMap.put("email", admins.getEmail());
		AAdmins aAdmin=this.adminsDao.findOne(condMap);
		if(aAdmin!=null){
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
			return resultJSON;
		}
		int res=this.adminsDao.save(admins);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	/*更新一个用户*/
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON=new JSONObject();
		Date dd=admins.getFaildTime();
		Map<String, Object> condMap=new HashMap<>();
		condMap.put("email", admins.getEmail());
		AAdmins aAdmins=this.adminsDao.findOne(condMap);
		if(aAdmins!=null&&aAdmins.getId()!=admins.getId()){
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
			return resultJSON ; 
		}
		int res=this.adminsDao.update(admins);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId());
			/* 影响的条数 */
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			
		}else{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	/*删除一个用户*/
	@Override
	public JSONObject deleteOneAdminsService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	/*查找一个用户*/
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		return this.adminsDao.findOne(condMap);
	}

	/*查询用户*/
	@Override
	public Map<String,Object> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 处理关键字的模糊匹配 */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") +  "%");
		}
		if(pageInfoUtil != null)
		{
			/* 分页查询 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = this.adminsDao.findList(condMap);
			/* 分页的两个要素 */
			/* 分页的总记录数 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			/* 设置值 */
			condMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			
			resultMap.put("list", adminsList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			resultMap.put("pageInfoUtil", pageInfoUtil);
			return resultMap ; 
		}
		List<AAdmins> adminsList = this.adminsDao.findList(condMap) ; 
		resultMap.put("list", adminsList);
		return resultMap;	
	}

	@Override
	public JSONObject saveOneRoleService(ARole role) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject deleteOneRoleService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
			/* 处理关键字的模糊匹配 */
			if(condMap.get("keyword") != null)
			{
				condMap.put("keyword", "%" + condMap.get("keyword") +  "%");
			}
			if(pageInfoUtil != null)
			{
				/* 分页查询 */
				Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
				List<ARole> roleList = this.roleDao.findList(condMap);
				/* 分页的两个要素 */
				/* 分页的总记录数 */
				pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
				return roleList ; 
			}
			return this.roleDao.findList(condMap);
	}

	/**
	 * 按天统计商品
	 */
	@Override
	public List<Map> statGoodsMapService(Map<String, Object> condMap) {

		return this.adminsDao.stat(condMap);

	}
}
